<?php
/**
 *
 * @author Strawberry🍓
 * @date 2021-06-02 3:28 下午
 *
 */

namespace app\api\controller;


use app\common\model\tang\FeedbackDetail;

class Feedback extends Base
{
    public function index()
    {
        $title = $this->request->param('title');
        $image = $this->request->param('image');
        $detail = $this->request->param('detail');
        if (empty($title)){
            return SendError("请填写标题");
        }
        if (empty($detail)){
            return SendError("请填写反馈的信息");
        }
        $model = new \app\common\model\tang\Feedback();
        $model->title = $title;
        $model->openid = $this->openid;
        if($image){
            $model->img    = $image;
        }
        $model->mobile = $this->mobile;
        $model->detail = $detail;
        $result = $model->save();
        if (empty($result)){
            return SendError("系统繁忙，稍后再试～");
        }
        return SendSuccess("success", []);
    }

    public function lists()
    {
        $info = \app\common\model\tang\Feedback::where('mobile',$this->mobile)->field('id,title,status,create_time')->select();
        foreach ($info as $key=>$value){
            $info[$key]['time'] = date('Y.m.d',$value['create_time']);
            unset($value['create_time']);
        }
        return SendSuccess("success", $info);
    }

    public function detail()
    {
        $id = $this->request->param('id');
        if (empty($id)){
            return SendError("参数不正确");
        }
        $info = \app\common\model\tang\Feedback::where('id',$id)->with(['details'])->find();
        $info['time'] = date('Y.m.d',$info['create_time']);
        $info['img']  = $info['img']?$this->request->domain(). $info['img']:'';
        if (!empty($info['details'])){
            foreach ($info['details'] as $key=>$value){
                $info['details'][$key]['time'] =  date('Y.m.d H:i',$info['create_time']);
            }
        }
        return SendSuccess("success", $info);
    }

    public function reply()
    {
        $id = $this->request->param('id');
        $detail = $this->request->param('detail');
        if (empty($id)){
            return SendError("参数不正确1");
        }
        if (empty($detail)){
            return SendError("参数不正确2");
        }
        $model =  new FeedbackDetail();
        $model->pid = $id;
        $model->mobile = $this->mobile;
        $model->openid = $this->openid;
        $model->detail = $detail;
        if ($model->save()){
            return SendSuccess("回复成功", []);
        }
        return SendError("回复失败稍后再试");
    }

}